package com.example.onlinetaxi.ui.activity.Driver.MainPage.MineActivities;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.onlinetaxi.LocalStroage.LocalStorage;
import com.example.onlinetaxi.R;
import com.example.onlinetaxi.dto.DataResponse;
import com.example.onlinetaxi.ui.activity.BaseActivity;
import com.example.onlinetaxi.util.ChartInitUtil.ChartInitUtil;
import com.example.onlinetaxi.util.ChartInitUtil.PieChartInitUtil;
import com.example.onlinetaxi.util.OKHttpUtil.GetDataResponse;
import com.example.onlinetaxi.util.OKHttpUtil.HttpUrl;
import com.example.onlinetaxi.util.OKHttpUtil.OKHttpUtil;
import com.example.onlinetaxi.view.Dialog.LoadingDialog;
import com.google.gson.Gson;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import lecho.lib.hellocharts.view.LineChartView;
import lecho.lib.hellocharts.view.PieChartView;

public class DriverMineWeeklyActivity extends Activity {
    // 图表初始化工具
    ChartInitUtil orderChartInitUtil = new ChartInitUtil();
    ChartInitUtil incomeChartInitUtil = new ChartInitUtil();
    PieChartInitUtil orderTypeChartInitUtil = new PieChartInitUtil();
    // 返回按钮
    private ImageView back;
    // 请求参数
    private Map<String, String> map = new HashMap<>();

    private LineChartView orderCountChart = null;
    private List<String> orderCountKey = null;
    private List<Double> orderCountValue = null;
    private Double allOrder = 0.0;

    private LineChartView incomeCountChart = null;
    private List<String> incomeCountKey = null;
    private List<Double> incomeCountValue = null;
    private Double allIncome = 0.0;
    private String allIncomeStr;

    private PieChartView orderTypeChart = null;
    private List<String> orderTypeKey = null;
    private List<Double> orderTypeValue = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

            Window window = getWindow();

            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS

                    | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);

            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN

                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION

                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);

            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);

            window.setStatusBarColor(Color.TRANSPARENT);

            window.setNavigationBarColor(Color.TRANSPARENT);

            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);//实现状态栏图标和文字颜色为暗色

        }

        setContentView(R.layout.activity_driver_mine_weekly);

        initView();

        initEvent();

        // 关闭加载框
        LoadingDialog.stopLoading();
    }

    private void initEvent() {
        orderChartInitUtil.initChart(orderCountChart, "最近7天总计完成" + allOrder.intValue() + "单", "订单个数", orderCountKey, orderCountValue, Color.parseColor("#9868a8"));
        incomeChartInitUtil.initChart(incomeCountChart, "最近7天总计收益" + allIncomeStr + "元", "费用", incomeCountKey, incomeCountValue, Color.parseColor("#f08300"));
        orderTypeChartInitUtil.initChart(orderTypeChart, "点击扇形区域显示订单情况~", orderTypeKey, orderTypeValue, allOrder.intValue());

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 关闭当前页面
                finish();
            }
        });
    }

    private void initView() {
        orderCountChart = findViewById(R.id.driver_order_count_chart);
        incomeCountChart = findViewById(R.id.driver_income_count_chart);
        orderTypeChart = findViewById(R.id.driver_order_type_chart);

        back = findViewById(R.id.driver_weekly_back_btn);

        // 从后端获取数据，初始化折线图
        getWeeklyOrderCount();
        getWeeklyIncomeCount();
        getWeeklyOrderType();
    }

    private void getWeeklyOrderType() {
        // 从后台获取用户上车地点统计数据
        map.clear();

        map.put("phone", LocalStorage.phone);

        // 获取用户不同订单的数量
        String response = OKHttpUtil.postAsyncRequest(HttpUrl.getBaseUrl(), map, "user", "getWeeklyOrderType");
        DataResponse dataResponse = GetDataResponse.getDataResponse(response);

        if (dataResponse.getCode().equals("200")) {
            Gson gson = new Gson();
            orderTypeKey = gson.fromJson(dataResponse.getData().get("orderTypeKey").toString(), ArrayList.class);
            orderTypeValue = gson.fromJson(dataResponse.getData().get("orderTypeValue").toString(), ArrayList.class);
        } else {
            Toast.makeText(this, "请求出错了！请联系管理员~", Toast.LENGTH_SHORT).show();
        }
    }

    private void getWeeklyIncomeCount() {
        // 从后台获取用户本周消费情况
        map.clear();

        map.put("phone", LocalStorage.phone);

        String response = OKHttpUtil.postAsyncRequest(HttpUrl.getBaseUrl(), map, "user", "getWeeklyIncomeCount");
        DataResponse dataResponse = GetDataResponse.getDataResponse(response);

        if (dataResponse.getCode().equals("200")) {
            Gson gson = new Gson();
            incomeCountKey = gson.fromJson(dataResponse.getData().get("incomeCountKey").toString(), ArrayList.class);
            incomeCountValue = gson.fromJson(dataResponse.getData().get("incomeCountValue").toString(), ArrayList.class);

            for (Double costCount : incomeCountValue) {
                // 计算全部订单
                allIncome += costCount;
            }
            DecimalFormat decimalFormat = new DecimalFormat("#.0");
            allIncomeStr = decimalFormat.format(allIncome);
        } else {
            Toast.makeText(this, "请求出错了！请联系管理员~", Toast.LENGTH_SHORT).show();
        }
    }

    private void getWeeklyOrderCount() {
        // 从后台获取用户本周订单
        map.clear();

        map.put("phone", LocalStorage.phone);

        // 获取用户完成订单数量
        String response = OKHttpUtil.postAsyncRequest(HttpUrl.getBaseUrl(), map, "user", "getWeeklyOrderCount");
        DataResponse dataResponse = GetDataResponse.getDataResponse(response);

        if (dataResponse.getCode().equals("200")) {
            Gson gson = new Gson();
            orderCountKey = gson.fromJson(dataResponse.getData().get("orderCountKey").toString(), ArrayList.class);
            orderCountValue = gson.fromJson(dataResponse.getData().get("orderCountValue").toString(), ArrayList.class);

            for (Double orderCount : orderCountValue) {
                // 计算全部订单
                allOrder += orderCount;
            }
        } else {
            Toast.makeText(this, "请求出错了！请联系管理员~", Toast.LENGTH_SHORT).show();
        }
    }
}